package com.gxa.learning.portal.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Project;
import com.gxa.learning.core.pojo.ProjectDetails;
import com.gxa.learning.core.pojo.Type;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.portal.mapper.TypeMapper;
import com.gxa.learning.portal.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TypeServiceImpl extends BaseServiceImpl<Type> implements TypeService {
    @Autowired
    private TypeMapper typeMapper = null;

    @Override
    public List<ProjectDetails> getProjectByType(Long tid, Integer page, Integer size) throws BusinessException {
        if (tid == null || tid <= 0) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_ERROR);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 5;
        }
        Integer offset = (page - 1) * size;
        Type type = typeMapper.selectByPrimaryKey(tid);
        if (type == null) {
            throw new BusinessException(ExceptionCode.ID_WRONG);
        }
        List<ProjectDetails> projects = typeMapper.getByType(tid, offset, size);
        if (projects == null || projects.size() == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DATA);
        }
        return projects;
    }

    @Override
    public Integer getProjectByTypeCount(Long tid) {
        return typeMapper.getByType(tid, null, null).size();
    }
}
